<?php

	

	//首先打开数据库 
	
	//1 导入配置文件 
	include("../../public/dbconfig.php");
	//2 连接数据库
	$link = @mysqli_connect(HOST,USER,PASS) or die("数据库连接失败");
	//3 选择数据库 设置字符集
	mysqli_set_charset($link,"utf8");
	mysqli_select_db($link,DBNAME);
	

	
	switch($_GET['a']){
		case "insert"://添加
		//接收表单传递过来的类别信息
		$type = $_POST['type'];
		$pid = $_POST['pid'];
		$path = $_POST['path'];

		//4 写sql语句 执行sql
		$sql = "insert into type(name,pid,path) values('$type','$pid','$path')";
		// echo $sql;
		// exit;
		mysqli_query($link,$sql);

		//5判断是否操作成功 
		if(mysqli_insert_id($link)>0){
			header("Location:index.php");
		}else{
			header("Location:add.php?errno=1");
		}

		break;


		case "del"://删除
        $id = $_GET['id'];
        echo "$id";
        $pid = $id;
        $sql = "select * from type where pid = {$pid}";
        $result = mysqli_query($link,$sql);
        $arr = mysqli_fetch_assoc($result);
        
        // var_dump($arr);die;
        if(!$arr){
            // var_dump(!$arr);die;
            $sql = "delete from type where id={$_GET['id']}";
            mysqli_query($link,$sql);
        
            header("Location:index.php");
                     
            
        }else{
            die("该类别下有子目录,无法删除!");
        }
        
		//4 写sql语句 执行sql
		

		//5判断是否操作成功 
		// if(mysqli_affected_rows($link)>0){
			// header("Location:index.php?errno=1");
		// }else{
			// header("Location:index.php?errno=2");
		// }
		break;


		case "update"://修改
		//接收表单传递过来的学生信息
        $id = $_POST['id'];
        // $pid = $_GET['pid'];
        // $path = $_GET['path'];
        $name = $_POST['name'];
		

		//4 写sql语句 执行sql
		$sql = "update type set name='{$name}' where id={$id}";
		mysqli_query($link,$sql);

		//5判断是否操作成功 
		// if(mysqli_affected_rows($link)>0){
        header("Location:index.php");
		// }else{
			// header("Location:edit.php?errno=1");
		// }
		break;
	}
	

	
	//6 关闭数据库  
	mysqli_close($link);

